module Volume

import IO;
import Crawl;
import String;
import List;
import Duplicate;
import ValueIO;

str newLine = "\r\n";

public int countLinesList(list[loc] locations)
{
	int returnVal = 0;
	for(loc l <- locations)
	{
		returnVal += countLinesFile(l);
	}
	return returnVal;
}


public int countLinesFile(loc location)
{
	//println("loc = <location>");
	return countLines(remExtras(readFile(location)));
}

public int countAllLines()
{
	println("counting");
	str allLines ="";
	allFiles = crawl(|file:///C:/Users/Renze/Downloads/SmallSql|, ".java");
	//allFiles = crawl(|file:///C:/Users/Renze/Downloads/hsqldb|, ".java");
	for(loc l <- allFiles)
	{
		allLines += readFile(l);
	}
	return countLines(remExtras(allLines));
}

public int countLines(str input)
{	
	return size(findAll(input, newLine)) + 1;	
}

public str remExtras(str input)
{
	return remEmptyLines(remCommentLines(remCommentBlocks(remAccolades(input))));
}

public str remCommentBlocks(str input)
{
	return visit(input){
   	case /<word:\/\*.*?\*\/>/s => ""   	
   	};
}

public str remCommentLines(str input)
{
	return visit(input){
   	case /<word:\/\/[^<newLine>]*<newLine>>/ => newLine   	
   	};
}

public str remEmptyLines(str input)
{
	input = trim(input);
	return visit(input){   	
	case /<word:<newLine>\s*<newLine>>/ => newLine 
   	};
}


public str remAccolades(str input)
{
	return visit(input){   	
	case /<newLine>\s*[\{\}]\s*<newLine>/ => newLine
   	};
}

public str getRanking()
{
	int volume = countAllLines();
	println("Volume: <volume>");
	
	if (volume <= 66000)
 		return "++";
 	else if (volume <= 246000)	
 		return "+";
 	else if (volume <= 665000)
 		return "o";
 	else if (volume <= 1310000)
 		return "-";
 	else
 		return "--";		
}
